<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/view/common/easycommon.jsp" %>
<!DOCTYPE html>
<html>
<head>
  <title>任务发布</title>
  <meta charset="utf-8">
  <style>
    table {
      font-size: 14px
    }
    a{
      text-decoration:none;
    }
  </style>
  <script type="text/javascript" src="${basepath}/assets/ztree/jquery.ztree.all-3.5.js"></script>
  <link rel="stylesheet" type="text/css" href="${basepath}/assets/ztree/zTreeStyle.css" />
  <script>
  	var rows;
    $(function () {    	
        var settingmenu = {
                data: {
                  simpleData: {
                    enable: true
                  }
                },
                check: {
                  enable: true,
                  nocheckInherit: true
                }
              };	
    	
      var datagrid; //定义全局变量datagrid
      var editRow = undefined; //定义全局变量：当前编辑的行
      datagrid = $('#dataTable').datagrid({
        rownumbers: true,
        //singleSelect:true,
        pagination: true,
        pageSize: 30,
        //singleSelect:false,
        fit: true, //datagrid自适应宽度
        fitColumn: false, //列自适应宽度
        striped: true, //行背景交换
        nowap: true, //列内容多时自动折至第二行
        border: false,
        method:'get',
        idField: 'id', //主键
        url: "${path}/cootask/queryCreateTask",
        columns: [[
          {field: '_operate', title: '操作', width: 300, formatter: formatOper},
          {field: 'title', title: '任务标题', width: 150},
          {field: 'content', title: '任务内容', width: 250},
          {field: 'taskTypeName', title: '任务类型', width: 80},
          {field: 'taskLevelName', title: '任务等级', width: 80},
          {field: 'createdtimeYMD', title: '创建时间', width: 100},
          {field: 'beginTime', title: '开始时间', width: 100},
          {field: 'endTime', title: '结束时间', width: 100},
          {field: 'transferMaxLevel', title: '最大流转次数', width: 100},
          {field: 'taskStatusName', title: '任务状态', width: 80},
          {field: 'note', title: '备注', width: 250}
        ]],
        //queryParams: { action: 'query' }, //查询参数
        toolbar:'#tb'
      });
      
      
      $.ajax({
          url: "${path}/commonSel/easyTreeDeptUser?dept_id=${deptId}",
          success: function (text) {
            $.fn.zTree.init($("#detptree"), settingmenu, text);
          },
          error: function (jqXHR, textStatus, errorThrown) {
            toastr.info("与远程服务器交互异常，请规范操作或重新登录", "提示", toastrOptsBottom);
          }
        });
      
    })
	
   function transPop() {
      $('#transWin').window('open');
    }
    
/*     function transPop(index) {
        $('#dataTable').datagrid('selectRow', index);
        var row = $('#dataTable').datagrid('getSelected');
        rows=row;
        console.log(rows);
      $('#transWin').window('open');
    } */
    
    function formatOper(val, row, index) {
      return '<a href="#" onclick="tablePop(' + index + ')">添加扩展字段</a>   ' +
              '<a href="#" onclick="detailPop(' + index + ')">查看流转情况</a>   ' +
              '<a href="#" onclick="editPop(' + index + ')">修改</a>   ' +
              '<a href="#" onclick="deletePop(' + index + ')">删除</a>   '+
       //       '<a href="#" onclick="transPop(' + index + ')">下发</a>   '+
              '<a href="#" onclick="operPop(' + index + ')">下发处理</a>';
              ;
    }
	
    var row;
    function operPop(index) {
      $('#dataTable').datagrid('selectRow', index);
      row = $('#dataTable').datagrid('getSelected');
      if (row) {
        $('#editForm2').form('clear');
        $('#editForm2').form('load', row);
        $('#editWin2').window('open');
      } else {
        $.messager.alert("提示", "请选择要编辑的行", "error");
      }
    }

    
    
    function newPop() {
      $('#editForm').form('clear');
      $('#editWin').window('open');
    }

    function editPop(index) {
      $('#dataTable').datagrid('selectRow', index);
      var row = $('#dataTable').datagrid('getSelected');
      if (row) {
        //console.log(row);
        $('#editForm').form('load', row);
        $('#editWin').window('open');
      } else {
        $.messager.alert("提示", "请选择要编辑的行", "error");
      }
    }

    function deletePop(index) {
      $('#dataTable').datagrid('selectRow', index);
      var row = $('#dataTable').datagrid('getSelected');
      if (row.id) {
        $.messager.confirm("提示", "你确定要删除吗?", function (r) {
          if (r) {
            $.ajax({
              type: "POST",
              url: "${path}/cootask/deleteTask",
              data: {id:row.id},
              dataType: 'json',
              //contentType: "application/json;charset=utf-8",
              success: function (text) {
                if (text.status == 1) {
                  layer.msg(text.msg, {time: 3000, icon: 6});
                  $('#dataTable').datagrid('reload');
                }
              },
              error: function (jqXHR, textStatus, errorThrown) {
                layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
              }
            });
          }
        });
      } else {
        $.messager.alert("提示", "请选择要删除的行", "error");
      }
    }

    function submitForm() {
      if ($('#editForm').form('enableValidation').form('validate')) {
    	  debugger;
        var formval = JsonCode(editForm);
        formval = JSON.parse(formval);
        //console.log(formval)
        var actionUrl = "${path}/cootask/saveTask";
        $.ajax({
          type: "POST",
          url: actionUrl,
          data: JSON.stringify(formval),
          dataType: 'json',
          contentType: "application/json;charset=utf-8",
          success: function (text) {
            if (text.status == 1) {
              $('#editForm').form('clear');
              $('#editWin').window('close');
              $('#dataTable').datagrid('reload');
            }
            layer.msg(text.msg, {time: 3000, icon: 6});
          },
          error: function (jqXHR, textStatus, errorThrown) {
            layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
          }
        });
      }
    }

    var layIndex;
    //添加扩展字段
    function tablePop(index){
      $('#dataTable').datagrid('selectRow', index);
      var row = $('#dataTable').datagrid('getSelected');
      if (row.id) {
        var url = "${path}/cootasktable/index?fTaskHeadid="+row.id;
        layIndex = layer.open({
          title:"扩展字段",
          type: 2,
          fix: false,
          maxmin: true,
          area: ['900px', '670px'],
          content: url,
          cancel: function(index){

          }
        });
      } else {
        $.messager.alert("提示", "请先选择一条记录", "error");
      }
    }

    //查看流转状态
    function detailPop(index){
      $('#dataTable').datagrid('selectRow', index);
      var row = $('#dataTable').datagrid('getSelected');
      if (row.id) {
        var url = "${path}/cootask/transDetail?id="+row.id;
        layIndex = layer.open({
          title:"流转明细",
          type: 2,
          fix: false,
          maxmin: true,
          area: ['900px', '740px'],
          content: url,
          cancel: function(index){

          }
        });
      } else {
        $.messager.alert("提示", "请先选择一条记录", "error");
      }
    }

    document.onkeydown=function(event){
      var e = event || window.event || arguments.callee.caller.arguments[0];
      if(e && e.keyCode==13){ // enter 键

      }
    }

 	var searchContent = function(){
       var seachform=$('#searchForm').form();
        $('#dataTable').datagrid('load',serializeObject(seachform)); 
 	}
 	
/*     function formatOper(val, row, index) {
        return '<a href="#" onclick="operPop(' + index + ')">查看/处理</a>   ';
                //'<a href="#" onclick="backFun(' + index + ')">退回</a>   ';
      } */
    
   // var row;
/*     function operPop(index) {
      $('#dataTable').datagrid('selectRow', index);
      row = $('#dataTable').datagrid('getSelected');
      if (row) {
        $('#editForm2').form('clear');
        $('#editForm2').form('load', row);
        $('#editWin2').window('open');
      } else {
        $.messager.alert("提示", "请选择要编辑的行", "error");
      }
    } */
    
    //下发
    function transSubmit(){
      var zTree = $.fn.zTree.getZTreeObj("detptree"),
              nodes = zTree.getChangeCheckedNodes(),
              checkedUserIds = [],checkedUserNames = [],
              checkedUserDeptIds = [],checkedUserDeptNames = [];
      for (var i=0, l=nodes.length; i<l; i++) {
        if(nodes[i].datatype=='u'){
          checkedUserIds.push(nodes[i].id);
          checkedUserNames.push(nodes[i].name);
          checkedUserDeptIds.push(nodes[i].deptId);
          checkedUserDeptNames.push(nodes[i].deptName);
        }
      }
      var rows=row;
      
      if (!rows.id) {
        return $.messager.alert("提示", "请先选择一条记录", "error");
      }
      if (checkedUserIds.length==0) {
        return $.messager.alert("提示", "请选择下发人员", "error");
      }

      if ($('#transForm').form('enableValidation').form('validate')) {
        var formval = JsonCode(transForm);
       
        
        formval = JSON.parse(formval);
        formval.id = rows.taskUserId;//流转id
        formval.fTaskHeadid = row.id;//任务id
        formval.fromUserId = rows.createUserId;//本级用户
        formval.fromUserName = rows.createUserName;
        formval.fromDeptId = rows.createDeptId;//本级部门
        formval.fromDeptName = rows.createDeptName;
        formval.toUserId = checkedUserIds.join(",");
        formval.toUserName = checkedUserNames.join(",");
        formval.toDeptId = checkedUserDeptIds.join(",");
        formval.toDeptName = checkedUserDeptNames.join(",");
        if(rows.transferMaxLevel){//如果存在最大次数限制，则设为1
         // if(parseInt(rows.transferLevel)<parseInt(rows.transferMaxLevel)){
          formval.transferLevel = 1;
         /*  }else{
            return $.messager.alert("提示", "任务流转已达最大层数，请完结任务", "error");
          } */
        }
        //console.log(formval);
        var actionUrl = "${path}/cootask/saveTaskUser";
        $.ajax({
          type: "POST",
          url: actionUrl,
          data: JSON.stringify(formval),
          dataType: 'json',
          contentType: "application/json;charset=utf-8",
          success: function (text) {
            if (text.status == 1) {
              $('#transWin').window('close');
              $('#editWin2').window('close');
              $('#dataTable').datagrid('reload');
            }
            layer.msg(text.msg, {time: 3000, icon: 6});
          },
          error: function (jqXHR, textStatus, errorThrown) {
            layer.msg("与远程服务器交互异常", {time: 3000, icon: 6});
          }
        });
      }
    }
    
    
  </script>
</head>
<body>
<table id="dataTable" title="创建任务" class="easyui-datagrid" style="width:850px;height:450px"
       singleSelect="true" iconCls="icon-save">
</table>

<div id="editWin" class="easyui-window" title="编辑" style="width:600px;height:600px;padding:10px;"
     data-options="iconCls:'icon-save',modal:true,closed:true,top:100">
  <form id="editForm" name="editForm" method="post">
    <input type="hidden" name="id"/>
    <table cellpadding="5">
      <tr>
        <td>标题:</td>
        <td><input class="easyui-textbox" type="text" name="title" style="width:150px"
                   data-options="required:true,prompt:'请填写标题'"/></td>
      </tr>
      <tr>
        <td>类型:</td>
        <td>
          <input class="easyui-combobox" id="taskType" name="taskType" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_type',
                            valueField: 'code',
                            textField: 'name',
                            required:true
                            ">
        </td>
      </tr>
      <tr>
        <td>等级:</td>
        <td>
          <input class="easyui-combobox" id="taskLevel" name="taskLevel" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_level',
                            valueField: 'code',
                            textField: 'name',
                            required:true
                            ">
        </td>
      </tr>
      <tr>
        <td>最大流转次数:</td>
        <td><input class="easyui-numberbox" type="text" name="transferMaxLevel" style="width:150px"
                   data-options="required:true,prompt:'只允许填写数字...'" value="0"/></td>
      </tr>
      
      <tr>
        <td>扩展字段模板</td>
        <td>
           <input class="easyui-combobox" id="tablebaseID" name="tablebaseID" style="width:150px;" 
           		data-options="
                            url:'${path}/cootasktable/tablebases',
                            valueField: 'id',
                            textField: 'name',
                          	method:'get',
                          	prompt:'将删除原有扩展字段'
                            ">
        </td>
      </tr>
      
      <tr>
        <td>开始时间:</td>
        <td>
          <input type="text" name="beginTime" id="beginTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
      </tr>
      <tr>
        <td>结束时间:</td>
        <td>
          <input type="text" name="endTime" id="endTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
      </tr>

      <tr>
        <td>内容:</td>
        <td><input class="easyui-textbox" name="content" data-options="multiline:true"
                   style="height:100px;width:400px"/></td>
      </tr>
      <tr>
        <td>任务备注:</td>
        <td><input class="easyui-textbox" name="note" data-options="multiline:true"
                   style="height:100px;width:400px"/></td>
      </tr>
    </table>
    <div style="text-align:right;margin-top:10px;padding-right:55px">
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:15%"
         onclick="submitForm()">保存</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" style="width:15%"
         onclick="javascript:$('#editWin').window('close')">关闭</a>
    </div>
  </form>
</div>

<div id="tb" > 
	<form id="searchForm"  style="height:13px">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newPop()">添加</a>  

	<input class="easyui-textbox" type="text" id="titles" name="title" style="width:150px"
                   data-options="prompt:'任务名'"/>
    <input class="easyui-combobox" id="taskTypes" name="taskType" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_type',
            valueField: 'code',
            textField: 'name',
			prompt:'任务类型'            
            ">
    <input class="easyui-combobox" id="taskLevels" name="taskLevel" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_level',
            valueField: 'code',
            textField: 'name',
			prompt:'任务等级'            
            ">                            
        
   	<input class="easyui-combobox" id="taskStatuss" name="taskStatus" style="width:150px;" 
    	data-options="
            url:'${path}/common/getBaseCodeList?type=coo_task_status',
            valueField: 'code',
            textField: 'name',
			prompt:'任务状态'            
            ">      
                          
    <input type="text" name="beginTime" id="sbeginTime" class="mytextbox" placeholder="开始时间"
              onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">    
    
    <input type="text" name="endTime" id="sendTime" class="mytextbox" placeholder="结束时间"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
	                     
    <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="searchContent()">搜索</a>  
	</form>
</div>

<div id="transWin" class="easyui-window" title="下发" style="width:680px;height:480px;padding:10px;"
     data-options="iconCls:'icon-save',modal:true,closed:true,top:100">
    <form id="transForm" name="transForm" method="post">
    <table cellpadding="5">
      <tr>
        <td>部门:</td>
        <td>流转备注:</td>
      </tr>
      <tr>
        <td>
          <div style="padding:10px;width:300px;height:300px;overflow-y:auto">
            <ul id="detptree" class="ztree"></ul>
          </div>
        </td>
        <td><input class="easyui-textbox" name="note" data-options="multiline:true"
                   style="height:300px;width:300px;"/></td>
      </tr>
    </table>
    <div style="text-align:right;margin-top:10px;padding-right:33px">
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" style="width:15%"
         onclick="transSubmit()">保存</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="width:15%"
         onclick="javascript:$('#transWin').window('close')">关闭</a>
    </div>
    </form>
</div>


<div id="editWin2" class="easyui-window" title="任务信息" style="width:620px;height:480px;padding:10px;"
     data-options="iconCls:'icon-save',modal:true,closed:true,top:100">
  <form id="editForm2" name="editForm2" method="post">
    <input type="hidden" name="id"/>
    <table cellpadding="5">
      <tr>
        <td>标题:</td>
        <td><input class="easyui-textbox" type="text" name="title" style="width:150px"/></td>
        <td>发起人:</td>
        <td><input class="easyui-textbox" type="text" name="createUserName" style="width:150px"/></td>
      </tr>
      <tr>
        <td>流转人:</td>
        <td><input class="easyui-textbox" type="text" name="fromUserName" style="width:150px"/></td>
        <td>接收人:</td>
        <td><input class="easyui-textbox" type="text" name="toUserName" style="width:150px"/></td>
      </tr>
      <tr>
        <td>类型:</td>
        <td>
          <input readonly="readonly"  class="easyui-combobox" id="taskType" name="taskType" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_type',
                            valueField: 'code',
                            textField: 'name',
                            required:false
                            ">
        </td>
        <td>等级:</td>
        <td>
          <input readonly="readonly"  class="easyui-combobox" id="taskLevel" name="taskLevel" style="width:150px;" data-options="
                            url:'${path}/common/getBaseCodeList?type=coo_task_level',
                            valueField: 'code',
                            textField: 'name',
                            required:false
                            ">
        </td>
      </tr>
      <tr>
        <td>最大流转次数:</td>
        <td><input readonly="readonly"  class="easyui-numberbox" type="text" name="transferMaxLevel" style="width:150px"
                   data-options="required:false" value="0"/></td>
        <td>当前流转次数:</td>
        <td><input  readonly="readonly"  class="easyui-numberbox" type="text" name="transferLevel" style="width:150px"
                   data-options="required:false" value="0"/></td>
      </tr>
      <tr>
        <td>开始时间:</td>
        <td>
          <input  readonly="readonly"  type="text" name="beginTime" id="beginTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
        <td>结束时间:</td>
        <td>
          <input  readonly="readonly"  type="text" name="endTime" id="endTime" class="mytextbox"
                 onFocus="WdatePicker({dateFmt: 'yyyy-MM-dd'})" style="width:150px">
        </td>
      </tr>
      <tr>
        <td>内容:</td>
        <td  colspan="3"><input  readonly="readonly"  class="easyui-textbox" name="content" data-options="multiline:true"
                   style="height:80px;width:420px"/></td>
      </tr>
      <tr>
        <td>任务备注:</td>
        <td colspan="3"><input  readonly="readonly"  class="easyui-textbox" name="note" data-options="multiline:true"
                   style="height:80px;width:420px"/></td>
      </tr>
    </table>
    <div style="text-align:right;margin-top:10px;padding-right:54px">
<!--       <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-tip'" style="width:30%;color:red"
         onclick="tablePop()">填写任务信息</a> -->
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-redo'" style="width:15%"
         onclick="transPop()">下发</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="width:20%"
         onclick="finish()">完结/提交</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" style="width:15%"
         onclick="javascript:$('#editWin2').window('close')">关闭</a>
    </div>
  </form>
</div>


</body>
</html>
